var fn_area_data={};
$.fn.area = function () {
    var _self = this;
    var province = _self.find(".province");
    var city = _self.find(".city");
    var county = _self.find(".county");
    var hideValue = _self.find(".hide-value");
	var get_area={};
    _self.initData={};
    var initComplete=0;
    var initCompleteTime = 3;
   
    _self.setprovince = function(){
        _self.getData(1,function(data){
            _self.setOption(1,province);
            // console.log("setprovince:",_self.initData);
            if(_self.initData.provinceID && initComplete<initCompleteTime){
                province.val(_self.initData.provinceID);
                province.trigger("change");
                initComplete++;
            }
        });
		
    }
	
    _self.setCity = function(pid){
		_self.setOption(pid,city,function(){
             if(_self.initData.cityID && initComplete<initCompleteTime){
                city.val(_self.initData.cityID);
                city.trigger("change");
                initComplete++;
             }
        });
    }
    _self.setcounty = function(pid){
		_self.setOption(pid,county,function(){
            if(_self.initData.countyID && initComplete<initCompleteTime){
                county.val(_self.initData.countyID);
                initComplete++;
             }
        });
    }
	
	_self.setOption=function(pid,object,callback){
		if(fn_area_data[pid]){
			var data=fn_area_data[pid];
             _self.setHtml(object,data,'请选择');
             callback?callback(data):'';
		}else{
			 _self.getData(pid,function(data){
                 _self.setHtml(object,data,'请选择');
                 callback?callback(data):'';
             });
			
		}
       
	}

	
    _self.setHtml = function(select,data,defaultName){
        if(data != null){
			select.html("<option value=''>"+defaultName+"</option>");
            $.each(data, function (k, area) {
                select.append("<option value='"+area.areaCode+"'>"+area.areaName+"</option>");
            });
        }
        else{
            select.append("<option value=''>"+defaultName+"</option>");
        }
    }
    _self.getData = function(pid,callback){
		if(get_area[pid]){
			}
        var domain =_self.attr("domain");
        var data = {};
        $.ajax({
            type: "post",
            // async :false,
            dataType:'json',
            url: domain+"api/index/render",
            data: {action:"getChilds",pid: pid},
            success: function (respose) {
                if(respose.success){
                    data = respose.data;
                    fn_area_data[pid]=data;
                    callback?callback(data):'';
                }
            }
        }); 
		get_area[pid]=data;
        return data;
    }
    _self.getDataReverse = function(aid,callback){
         var domain =_self.attr("domain");
        var data = {};
        $.ajax({
            type: "post",
            // async :false,
            dataType:'json',
            url: domain+"api/index/render",
            data: {action:"getReverse",aid: aid},
            success: function (respose) {
                if(respose.success){
                    data = respose.data;
                    callback?callback(data):'';
                }
            }
        });
        return data;
    }
    _self.init = function(){
        _self.setprovince();
        var aid = parseInt($.trim(hideValue.val()));

        if(aid != 0 && !isNaN(aid)){
             
            _self.getDataReverse(aid,function(data){
                _self.initData = data;
                if(data.countyID){
                    initCompleteTime=3;
                }else{
                    initCompleteTime = 2;
                }
            });
           
        }
    }

    province.change(function(){
        var pid = $(this).val();
        // if(pid){
        //     $(this).attr("data-id",pid);
        //     var data_id = pid;
        // }else{
        //      var data_id = $(this).attr("data-id");
        // }
        city.empty();
        county.empty();
        // hideValue.val("");
        _self.setCity(pid);
         hideValue.val(pid);
        city.trigger("change");
    });
    city.change(function(){
        var pid = $(this).val();
        county.empty();
        // hideValue.val("");
        _self.setcounty(pid);
        hideValue.val(pid);
        county.trigger("change");
    });
    county.change(function(){
        var id = $(this).val();
        hideValue.val(id);
    });
    _self.init();
    return _self;
}